package com.aqielife.aqiemusic;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import com.alibaba.android.arouter.launcher.ARouter;
import com.aqielife.aqiemusic.performance.launchstarter.TaskDispatcher;
import com.aqielife.aqiemusic.performance.tasks.ARouterTask;
import com.aqielife.aqiemusic.performance.tasks.AudioTask;
import com.aqielife.aqiemusic.performance.tasks.MyMainTask;
import com.aqielife.aqiemusic.performance.tasks.ShareTask;
import com.aqielife.aqiemusic.performance.tasks.UpdateTask;
import com.aqielife.aqiemusic.performance.tasks.VideoTask;
import com.aqielife.aqiemusic.performance.timeCounter.LaunchTimer;
import com.aqielife.lib_audio.app.AudioHelper;
import com.aqielife.lib_share.ShareManager;
import com.aqielife.lib_update.app.UpdateHelper;
import com.aqielife.lib_video.app.VideoHelper;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class MusicApplication extends Application {
    private static MusicApplication mMusicApplication = null;
    final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));
    final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;

    private  CountDownLatch mCountDownLatch = new CountDownLatch(1);

    @Override
    public void onCreate() {
        super.onCreate();
        mMusicApplication = this;
        // 线程优化
        ExecutorService service = Executors.newFixedThreadPool(CORE_POOL_SIZE);

        LaunchTimer.startRecord();
        initAllTask();
        LaunchTimer.endRecord();
        // initAll(service);
        /**
         * 这段代码会导致谷歌手机崩溃
         * 添加hook
         */
        /*DexposedBridge.hookAllConstructors(ImageView.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                DexposedBridge.findAndHookMethod(ImageView.class, "setImageBitmap", Bitmap.class, new ImageHook());
            }
        });*/

    }

    private void initAllTask() {
        TaskDispatcher.init(mMusicApplication);
        TaskDispatcher dispatcher = TaskDispatcher.createInstance();
        dispatcher.addTask(new AudioTask())
                .addTask(new ShareTask())
                .addTask(new UpdateTask())
                .addTask(new VideoTask())
                .addTask(new ARouterTask())
                .addTask(new MyMainTask())
                .start();
        // 启动器等待方法 复写 needWait(),必须在onCreated 里启动完成
        dispatcher.await();
    }

    // 初级版 线程池优化启动
    private void initAll(ExecutorService service) {
        LaunchTimer.startRecord();
        service.submit(new Runnable() {
            @Override
            public void run() {
                //音频SDK初始化
                AudioHelper.init(mMusicApplication);
            }
        });
        service.submit(new Runnable() {
            @Override
            public void run() {
                //分享SDK初始化
                ShareManager.initSDK(mMusicApplication);
            }
        });
        service.submit(new Runnable() {
            @Override
            public void run() {
                //视频SDK初始化
                VideoHelper.init(mMusicApplication);
            }
        });
        service.submit(new Runnable() {
            @Override
            public void run() {
                //ARouter初始化
                ARouter.init(mMusicApplication);
            }
        });
        service.submit(new Runnable() {
            @Override
            public void run() {
                //更新组件下载
                UpdateHelper.init(mMusicApplication);
            }
        });

        service.submit(new Runnable() {
            @Override
            public void run() {
                 _init();
                 mCountDownLatch.countDown();
            }
        });
        try {
            mCountDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LaunchTimer.endRecord();
    }

    private void _init() {
        Handler handler = new Handler(Looper.getMainLooper());
    }

    public static MusicApplication getInstance(){
        return mMusicApplication;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // 对线程 堆栈信息hook
        /*DexposedBridge.hookAllConstructors(Thread.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                Thread thread = (Thread) param.thisObject;
                // 拿到线程名字
                LogUtils.i(thread.getName()+" stack "+ Log.getStackTraceString(new Throwable()));
            }
        });*/
    }
}
